package com.shujia.mr.combine;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.CombineTextInputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

/**
 * 如果需要在集群中执行，那么对应输入输出的路径需要做修改，修改成HDFS中的路径
 *  执行命令为：
 *      hadoop jar hadoopCode-1.0.jar com.shujia.mr.wordcount.MyCombineDriver
 */
public class MyCombineDriver {
    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
        Configuration conf = new Configuration();

        // 一个MapReduce程序就是一个Job
        Job job = Job.getInstance(conf);
        job.setJarByClass(MyCombineDriver.class);
        job.setJobName("combine");

        // 设置Map和Reduce过程的处理类
        job.setMapperClass(MyCombineMapper.class);
        job.setReducerClass(MyCombineReducer.class);

        // 设置Map阶段输出的Key和Value的数据类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);

        // 设置最终输出的Key和Value的数据类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);


        // INPUT_FORMAT_CLASS_ATTR = "mapreduce.job.inputformat.class";
        job.setInputFormatClass(CombineTextInputFormat.class);
        // 设置blockSize大小,小于该大小的都为小文件
        CombineTextInputFormat.setMaxInputSplitSize(job,4194304);


        // 设置输入输出的路径
        FileInputFormat.addInputPath(job,new Path("data/combine"));
        FileOutputFormat.setOutputPath(job,new Path("output/combine"));


        // 提交Job并执行
        job.waitForCompletion(true);

    }
}
